package cn.iocoder.yudao.module.system.service.customerservice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.system.controller.admin.customerservice.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.customerservice.CustomerServiceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.customerservice.CustomerServiceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 客户服务 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class CustomerServiceServiceImpl implements CustomerServiceService {

    @Resource
    private CustomerServiceMapper customerServiceMapper;
    @Resource
    private FileApi fileApi;

    @Override
    public Integer createCustomerService(CustomerServiceSaveReqVO createReqVO) {
        // 插入
        CustomerServiceDO customerService = BeanUtils.toBean(createReqVO, CustomerServiceDO.class);
        customerServiceMapper.insert(customerService);
        // 返回
        return customerService.getId();
    }

    @Override
    public void updateCustomerService(CustomerServiceSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerServiceExists(updateReqVO.getId());
        // 更新
        CustomerServiceDO updateObj = BeanUtils.toBean(updateReqVO, CustomerServiceDO.class);
        customerServiceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomerService(Integer id) {
        // 校验存在
        validateCustomerServiceExists(id);
        // 删除
        customerServiceMapper.deleteById(id);
    }

    private void validateCustomerServiceExists(Integer id) {
        if (customerServiceMapper.selectById(id) == null) {
            throw exception(CUSTOMER_SERVICE_NOT_EXISTS);
        }
    }

    @Override
    public CustomerServiceDO getCustomerService(Integer id) {
        CustomerServiceDO data = customerServiceMapper.selectById(id);
        attachUrl(data);
        return data;
    }

    @Override
    public PageResult<CustomerServiceDO> getCustomerServicePage(CustomerServicePageReqVO pageReqVO) {
        PageResult<CustomerServiceDO> result = customerServiceMapper.selectPage(pageReqVO);
        attachUrl(result.getList());
        return result;
    }

    private void attachUrl(CustomerServiceDO data) {
        if (data == null) {
            return;
        }

        attachUrl(Arrays.asList(data));
    }

    private void attachUrl(List<CustomerServiceDO> dataList) {
        if (CollUtil.isEmpty(dataList)) {
            return;
        }

        dataList.parallelStream().forEach(data -> {
            if (StrUtil.isNotEmpty(data.getIconPath())) {
                final String url = fileApi.getTempDownloadUrl(null, data.getIconPath(), null);
                data.setIconUrl(url);
            }

            if (StrUtil.isNotEmpty(data.getQrcodePath())) {
                final String url = fileApi.getTempDownloadUrl(null, data.getQrcodePath(), null);
                data.setQrcodeUrl(url);
            }
        });
    }

}